﻿<Window x:Class="wpf_ImageSearch.View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:prism="http://www.codeplex.com/prism"
        xmlns:prismsupporti="clr-namespace:Okazuki.MVVM.PrismSupport.Interactivity;assembly=Okazuki.MVVM.PrismSupport"
        xmlns:v="clr-namespace:wpf_ImageSearch.View"
        xmlns:vm="clr-namespace:wpf_ImageSearch.ViewModel"
        Title="Yahoo!画像検索クライアント"
        DataContext="{StaticResource MainWindowViewModel}"
        Left="{Binding Settings.Left, Mode=TwoWay}"
        Top="{Binding Settings.Top, Mode=TwoWay}"
        Width="{Binding Settings.Width, Mode=TwoWay}"
        Height="{Binding Settings.Height, Mode=TwoWay}">

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Closing">
            <ei:CallMethodAction TargetObject="{Binding Settings}" MethodName="Save"/>
        </i:EventTrigger>

        <prism:InteractionRequestTrigger SourceObject="{Binding ShowPreviewRequest}">
            <prismsupporti:ShowWindowAction WindowType="{x:Type v:ImagePreviewWindow}"/>
        </prism:InteractionRequestTrigger>
        
        <prism:InteractionRequestTrigger SourceObject="{Binding ShowErrorMessageRequest}">
            <prismsupporti:ShowMessageBoxAction Button="OK" Icon="Exclamation"/>
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>

        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>

            <TextBox Grid.Column="0" Text="{Binding SearchQuery, UpdateSourceTrigger=PropertyChanged}">
                <i:Interaction.Triggers>
                    <ei:KeyTrigger Key="Return" ActiveOnFocus="True">
                        <i:InvokeCommandAction Command="{Binding Search}"/>
                    </ei:KeyTrigger>
                </i:Interaction.Triggers>
            </TextBox>
            
            <Button Grid.Column="1" Content="検索(_S)" Command="{Binding Search}"/>
        </Grid>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>

            <StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center">
                <TextBlock Text="全"/>
                <TextBlock Text="{Binding AvailableCount}"/>
                <TextBlock Text="件"/>
            </StackPanel>

            <Button Grid.Column="1" Content="次を取得(_N) >" VerticalAlignment="Center" Command="{Binding GetNext}"/>
        </Grid>

        <ListBox Grid.Row="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled"  SelectionMode="Single" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDoubleClick">
                    <i:InvokeCommandAction Command="{Binding ShowPreview}"/>
                </i:EventTrigger>
                <ei:KeyTrigger Key="Return" ActiveOnFocus="True">
                    <i:InvokeCommandAction Command="{Binding ShowPreview}"/>
                </ei:KeyTrigger>
            </i:Interaction.Triggers>
            
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel IsItemsHost="True">
                        <i:Interaction.Behaviors>
                            <ei:FluidMoveBehavior AppliesTo="Children" Duration="0:0:0.5" >
                            	<ei:FluidMoveBehavior.EaseY>
                            		<CubicEase EasingMode="EaseInOut"/>
                            	</ei:FluidMoveBehavior.EaseY>
                            	<ei:FluidMoveBehavior.EaseX>
                            		<CubicEase EasingMode="EaseInOut"/>
                            	</ei:FluidMoveBehavior.EaseX>
                            </ei:FluidMoveBehavior>
                        </i:Interaction.Behaviors>
                    </WrapPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>

            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical" Margin="2,2,2,2">
                        <Image Width="{Binding Path=Settings.ThumbnailSize, Source={StaticResource MainWindowViewModel}}"
                            Height="{Binding Path=Settings.ThumbnailSize, Source={StaticResource MainWindowViewModel}}"
                            Source="{Binding Thumbnail.Url}"
                            Stretch="Uniform"/>

                        <TextBlock TextAlignment="Center"
                                TextTrimming="CharacterEllipsis"
                                Text="{Binding Title}"
                                Width="{Binding Source={StaticResource MainWindowViewModel}, Path=Settings.ThumbnailSize}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <Grid Grid.Row="3">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="auto"/>
            </Grid.ColumnDefinitions>

            <Slider Grid.Column="0"
                    Maximum="170" Minimum="1"
                    Value="{Binding Settings.ThumbnailSize}"
                    Width="100"
                    HorizontalAlignment="Left"
                    Margin="0,4,0,0"
                    AutoToolTipPlacement="TopLeft"/>

            <Image Grid.Column="1"
                   Source="http://i.yimg.jp/images/yjdn/yjdn_attbtn1_125_17.gif"
                   Margin="15,4,15,15"
                   Cursor="Hand">
                <!--↑MarginはYahooの規定↑-->
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseDown">
                        <!--これもYahooの規定-->
                        <prismsupporti:StartProcessAction FileName="http://developer.yahoo.co.jp/about" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Image>
        </Grid>
    </Grid>
</Window>
